package com.weave.controller;

import com.weave.model.GameModel;
import com.weave.view.GameFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class GameController {
    private final GameModel model;
    private final GameFrame view;
    private StringBuilder currentInput = new StringBuilder();

    public GameController(GameModel model, GameFrame view) {
        this.model = model;
        this.view = view;

        initializeController();
    }

    private void initializeController() {
        attachKeyboardListeners();
        attachButtonListeners();
        setupPhysicalKeyboard();
    }

    private void attachKeyboardListeners() {
        Component[] components = view.getKeyboard().getComponents();
        for (Component comp : components) {
            if (comp instanceof JPanel) {
                JPanel rowPanel = (JPanel) comp;
                for (Component btnComp : rowPanel.getComponents()) {
                    if (btnComp instanceof JButton) {
                        JButton button = (JButton) btnComp;
                        button.addActionListener(e -> handleButtonInput(button.getText()));
                    }
                }
            }
        }
    }

    private void setupPhysicalKeyboard() {
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
                .addKeyEventDispatcher(e -> {
                    if (e.getID() == KeyEvent.KEY_TYPED) {
                        handleKeyInput(e.getKeyChar());
                    }
                    return false;
                });
    }

    private void handleButtonInput(String input) {
        processCharacter(input.charAt(0));
    }

    private void handleKeyInput(char c) {
        if (Character.isLetter(c)) {
            processCharacter(Character.toUpperCase(c));
        }
    }

    private void processCharacter(char c) {
        if (currentInput.length() < GameModel.WORD_LENGTH) {
            currentInput.append(c);
            updateInputDisplay();

            if (currentInput.length() == GameModel.WORD_LENGTH) {
                submitWord();
            }
        }
    }


    private void updateInputDisplay() {
        view.setInputPreview(currentInput.toString());
    }


    private void submitWord() {
        try {
            model.processInput(currentInput.toString());
            resetCurrentInput();
        } catch (IllegalArgumentException ex) {
            handleInvalidInput(ex.getMessage());
        }
    }

    private void resetCurrentInput() {
        currentInput.setLength(0);
        updateInputDisplay();
    }

    private void handleInvalidInput(String message) {
        JOptionPane.showMessageDialog(view, message, "Invalid Input", JOptionPane.ERROR_MESSAGE);
        resetCurrentInput();
    }

    // 按钮事件处理
    private void attachButtonListeners() {
        view.getResetButton().addActionListener(e -> {
            model.initializeGame(false);
            resetCurrentInput();
        });

        view.getNewGameButton().addActionListener(e -> {
            model.initializeGame(true);
            resetCurrentInput();
        });
    }
}